package processingdata;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;

public class LocationComputer {
	
	public static void main(String[] args)throws IOException
	{
		String inputDir="project3cs424/geofound";
		ArrayList<String> inputFiles=new ArrayList<String>();
		getFilesInDirectory(inputDir, inputFiles);
		
		HashMap<String,String> locationMap=new HashMap<String,String>();
		
		for(int i=0;i<inputFiles.size();i++)
		{
			File file=new File(inputFiles.get(i));
			BufferedReader inputReader=new BufferedReader(new FileReader(file));
			while(inputReader.ready())
			{
				String inputLine=inputReader.readLine();
				String inputLineParts[]=inputLine.split("###");
				String city=inputLineParts[1];
				String state=inputLineParts[2];
				String latitudeAndLongitude=inputLineParts[inputLineParts.length-2]+"\t"+inputLineParts[inputLineParts.length-1];
				if(!locationMap.containsKey(city+"###"+state))
				{
					locationMap.put(city+"###"+state, latitudeAndLongitude);	
				}
				
				
			}
		}
		
		String splitInputDir="project3cs424/new_split_files";
		ArrayList<String> splitFiles=new ArrayList<String>();
		getFilesInDirectory(splitInputDir, splitFiles);
		
		BufferedWriter outputWriter=new BufferedWriter(new FileWriter(new File("data/ufonew.data")));
		for(int j=0;j<splitFiles.size();j++)
		{
			File file=new File(splitFiles.get(j));
			BufferedReader inputReader=new BufferedReader(new FileReader(file));
			while(inputReader.ready())
			{
				String inputLine=inputReader.readLine();
				String[] inputLineParts=inputLine.split("\t");
				String city=inputLineParts[1];
				String state=inputLineParts[2];
				String latitudeAndLongitude=locationMap.get(city+"###"+state);
				String outputLatitudeAndLongitude=latitudeAndLongitude;
				String outputLine=inputLine+"\t"+outputLatitudeAndLongitude;
				outputWriter.write(outputLine+"\n");
			}
		}
		outputWriter.close();
		
	}
	
	public static void getFilesInDirectory(String dirName,ArrayList<String> files)
	{
		File dir=new File(dirName);
		
		//System.out.println(dir);
		if(dir.isDirectory())
		{
			if(dir.getName().contains(".svn"))
			{
				//do nothing
			}
			else
			{
			
				File[] fileArray=dir.listFiles();
							
				for(int i=0;i<fileArray.length;i++)
				{
				//	System.out.println(fileArray[i]);
					getFilesInDirectory(fileArray[i].getAbsolutePath(),files);
				}

			}
					}
		else if(dir.isFile())
		{
			files.add(dir.getAbsolutePath());
		}
		
		
	}

}
